/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'calendar'
], function( $, common, ready, Collection, template, Model, dialog ,calendar) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                condition: {},
                url: _global.url.api + 'anhui_click',
                callback: function() {
                    loaded();
                }
            });
            $.ajax({
                url: _global.url.api + 'anhui_click_show',
                type: 'get',
                async: false,
                success: function(res) {
                    if(res.code == 0) {
                        console.log(res);
                        $('#sell-view').html(template('sell-view-tpl', {'click':res.click_nums,'amount': res.total_amount,'sale_num':res.sale_nums,'goods_amount_per':res.goods_amount_pers}));
                    }
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('.form_datetime').calendar();
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                $.ajax({
                    url: _global.url.api + 'anhui_click_show',
                    type: 'get',
                    async: false,
                    data:{'start_time':$("#start_time").val(),'end_time':$("#end_time").val()},
                    success: function(res) {
                        if(res.code == 0) {
                            console.log(res);
                            $('#sell-view').html(template('sell-view-tpl', {'click':res.click_nums,'amount': res.total_amount,'sale_num':res.sale_nums,'goods_amount_per':res.goods_amount_pers}));
                        }
                    }
                });
                _this.collection.condition.status = $("input[name='show']:checked").val();
                _this.collection.condition.start_time = $("#start_time").val();
                _this.collection.condition.end_time = $("#end_time").val();
                _this.collection.reload();
            });
        }
    };

    new index();

});